﻿@using BusinessLibrary
@model IEnumerable<MMSWebsite.Models.Asset.Asset>

@{
    ViewBag.Title = "Detail & Asset";
    double sumLoan = 0, sumValuation = 0;
}
<table>
    <tr>
        <th>@Html.DisplayNameFor(model => model.AssetName)</th>
        <th>@Html.DisplayNameFor(model => model.Information)</th>
        <th>@Html.DisplayNameFor(model => model.AssetType)</th>
        <th>@Html.DisplayNameFor(model => model.Valuation)</th>
        <th>@Html.DisplayNameFor(model => model.Valuation.ValuationBy)</th>
        <th>@Html.DisplayNameFor(model => model.Loan)</th>
        <th>@Html.DisplayNameFor(model => model.Rate)</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => item.AssetName)
            </td>
            <td>@Html.DisplayFor(model => item.Information)</td>
            <td style="text-align: center;">@Html.DisplayFor(model => item.AssetType.name)</td>
            @if(item.Valuation.Value!=null)
            {
                sumValuation += double.Parse(item.Valuation.Value.ToString());
            }
            <td>@Html.DisplayFor(model => item.Valuation.Value)</td>
            <td>@Html.DisplayFor(model => item.Valuation.ValuationBy.Employee.Name) (@Html.DisplayFor(model => item.Valuation.ValuationBy.Employee.Code)) </td>
            @if(item.Loan.ValueLoan!=null)
            {
                sumLoan += double.Parse(item.Loan.ValueLoan.ToString());
            }
            <td>@Html.DisplayFor(model => item.Loan.ValueLoan)</td>
            <td>@Html.DisplayFor(model => item.Rate.ValueRate)</td>
        </tr>
    }
    <tr>
        <td colspan="3" style="text-align: right; font-weight: bold">Sum</td>
        <td><span id="sumValualtion">@sumValuation</span></td>
        <td></td>
        <td><span id="sumLoan">@sumLoan</span></td>
        <td></td>
    </tr>
</table>
<script type="text/javascript">
    $(window).load(function () {
        $('#sumValualtion').text(formatCurrency($('#sumValualtion').text()));
        $('#sumLoan').text(formatCurrency($('#sumLoan').text()));
    });
    function formatCurrency(num) {
        num = num.toString().replace(/\$|\,/g, '');
        if (isNaN(num))
            num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
                num.substring(num.length - (4 * i + 3));
        return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    }
    //  End -->
</script>